package codeTop;

public class q110 {
    public boolean isBalanced(TreeNode root) {
        dfs(root);
        return flag;
    }
    boolean flag = true;
    int dfs(TreeNode node){
        if (!flag) {
            return 0;
        }
        if (node == null) {
            return 0;
        }
        int left = dfs(node.left);
        int right = dfs(node.right);
        if (Math.abs(left - right) > 1) {
            flag = false;
            return 0;
        }
        return Math.max(left,right)+1;
    }
}
